package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.Receipt;
import com.ruoyi.system.service.IContractService;
import com.ruoyi.system.service.IReceiptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收款单 信息操作处理
 *
 * @author ruoyi
 * @date 2018-12-24
 */
@Controller
@RequestMapping("/system/receipt")
public class ReceiptController extends BaseController {
    private String prefix = "system/receipt" ;

    @Autowired
    private IReceiptService receiptService;
    @Autowired
    private IContractService contractService;

    @RequiresPermissions("system:receipt:view")
    @GetMapping()
    public String receipt() {
        return prefix + "/receipt" ;
    }

    /**
     * 查询收款单列表
     */
    @RequiresPermissions("system:receipt:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Receipt receipt) {
        startPage();
        List<Receipt> list = receiptService.selectReceiptList(receipt);
        return getDataTable(list);
    }


    /**
     * 导出收款单列表
     */
    @RequiresPermissions("system:receipt:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Receipt receipt) {
        List<Receipt> list = receiptService.selectReceiptList(receipt);
        ExcelUtil<Receipt> util = new ExcelUtil<Receipt>(Receipt.class);
        return util.exportExcel(list, "receipt");
    }

    /**
     * 新增收款单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {

        mmap.put("contractList", contractService.selectContractList(null));
        return prefix + "/add" ;
    }

    /**
     * 新增保存收款单
     */
    @RequiresPermissions("system:receipt:add")
    @Log(title = "收款单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Receipt receipt) {
        System.out.println("11111");
        return toAjax(receiptService.insertReceipt(receipt));
    }

    /**
     * 修改收款单
     */
    @GetMapping("/edit/{receiptId}")
    public String edit(@PathVariable("receiptId") Integer receiptId, ModelMap mmap) {
        Receipt receipt = receiptService.selectReceiptById(receiptId);
        mmap.put("receipt", receipt);
        return prefix + "/edit" ;
    }

    /**
     * 修改保存收款单
     */
    @RequiresPermissions("system:receipt:edit")
    @Log(title = "收款单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Receipt receipt) {
        return toAjax(receiptService.updateReceipt(receipt));
    }

    /**
     * 删除收款单
     */
    @RequiresPermissions("system:receipt:remove")
    @Log(title = "收款单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(receiptService.deleteReceiptByIds(ids));
    }

}
